package com.mes.web.controller.lz;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mes.utils.StringUtil;
import com.mes.entity.cb.Materiel;
import com.mes.entity.lc.MaterialStock;
import com.mes.entity.lz.BOM;
import com.mes.entity.lz.ProductUnionMaterial;
import com.mes.service.hy.IMaterService;
import com.mes.service.lz.BOM2Service;
import com.mes.utils.ResponseUtil;
/**
 * 
 * @ClassName:BOM2Controller
 * @Description:
 * @Author lz
 * @Date 2018年1月25日 16:00:53
 * @Version 1.0
 */
@Controller
public class BOM2Controller {
	@Resource
	BOM2Service bom2Service;
	
	//hy
	@Resource
	private IMaterService iMaterService;
	MaterialStock materialStock=null;
	
	BOM bomEntity = null;
	/**
	 * 
	 * @param pNummArr 产品编号数组
	 * @param response
	 * @param request
	 * @throws Exception
	 */
	@RequestMapping("/queryProductUnionMaterial")
	public void queryProductUnionMaterial(@RequestParam(value = "pNummArr[]") String[] pNummmArr, String lingliaoOrdNum, String plannum, HttpServletResponse response, HttpServletRequest request) throws Exception{
		/*如果选中了数组，没选中不进该方法*/
		if (pNummmArr.length > 0) {
			/*根据选中的产品编号查询联合表中该产品对应的物料编号*/
			 for (int i = 0; i < pNummmArr.length; i++) {
				List<ProductUnionMaterial> productUnionMaterialList = bom2Service.queryProductUnionMaterial(pNummmArr[i]);
				/*注：虽然为List,实际情况只取第一个实体*/
				/*如果根据产品编号查询对应的物料编号*/
				if (productUnionMaterialList.size() > 0) {
					String strArr1 = productUnionMaterialList.get(0).getMaterieNum();
					/*类似于这样的字符串*/
					// String strArr = "124571,357894,39751,15472,2546";
					/*如果查到，先将字符串分割，再用分割后的物料编号去查询物料表*/
					String[] strArr2 = strArr1.split(",");
					for (int j = 0; j < strArr2.length; j++) {
						/*根据分割后生成的物料编号查询物料表*/
						// System.out.println(strArr2[j]);
						/*注：虽然为List,实际情况只取第一个实体*/
						List<Materiel> materielList = bom2Service.queryMaterial(strArr2[j]);
						/*如果根据物料编号查到*/
						if (materielList.size() > 0) {
							bomEntity = new BOM();
							/*将物料编号写入BOM表中的remark01字段*/
							bomEntity.setRemark01(materielList.get(0).getNumber());
							/*根据BOM中的物料编号查询*/
							List<BOM> bomList = bom2Service.queryBOM(bomEntity.getRemark01());
							/*如果存在*/
							if (bomList.size() > 0) {
								/*物料名称(查询cb物料表获得1)*/
								bomEntity.setMaterialName(materielList.get(0).getName());
								/*物料型号(查询cb物料表获得2)*/
								bomEntity.setMaterialType(materielList.get(0).getMaterialmodel());
								/*计量单位(查询cb物料表获得3)*/
								bomEntity.setMaterialUnit(materielList.get(0).getCompany());
								/*需求数量(排产单hy中已经存在的4)*/
								bomEntity.setRequireCount(plannum);
								/*现编库库存(根据物料id查询jy线边库表获得)*/
								String nowma = bom2Service.queryJYnowma(String.valueOf(materielList.get(0).getId()));
								System.out.println(nowma + "BBBBBBBBBB");
								if (StringUtil.isNotEmpty(nowma)) {
									bomEntity.setXianbianCount(nowma);
								}else{
									bomEntity.setXianbianCount(null);
								}
								/*安全库存(查询cb物料表获得6)*/
								bomEntity.setSafeCount(materielList.get(0).getRemark4());
								/*将领料单号和BOM绑定7*/
								if (StringUtil.isNotEmpty(lingliaoOrdNum)) {
									bomEntity.setRemark02(lingliaoOrdNum);
								}else{
									bomEntity.setRemark02(null);
								}
								/*领料需求量((需求数量*损耗率)-线边库库存)8*/
								bomEntity.setLingliaoRequireCount(String.valueOf((Integer.parseInt(plannum)-Integer.parseInt(nowma))));
								
								/**
								 * hy根据物料名称先查出其id再去lc的materialStock表中查询条码、批次号
								 */
								materialStock=iMaterService.sel(iMaterService.selMid(bomEntity.getMaterialName()));
								bomEntity.setRemark05(materialStock.getWremark1());//批次号
								bomEntity.setRemark06(materialStock.getWremark2());//条形码
								
								/*根据物料编号查询到就去修改*/
								bom2Service.savaAndUpdate(bomEntity);
							}else{
								/*物料名称(查询cb物料表获得)*/
								bomEntity.setMaterialName(materielList.get(0).getName());
								/*物料型号(查询cb物料表获得)*/
								bomEntity.setMaterialType(materielList.get(0).getMaterialmodel());
								/*计量单位(查询cb物料表获得)*/
								bomEntity.setMaterialUnit(materielList.get(0).getCompany());
								/*需求数量(排产单hy中已经存在的)*/
								bomEntity.setRequireCount(plannum);
								/*现编库库存(根据物料id查询jy线边库表获得)*/
								String nowma = bom2Service.queryJYnowma(String.valueOf(materielList.get(0).getId()));
								System.out.println(nowma + "AAAAAAAAA");
								if (StringUtil.isNotEmpty(nowma)) {
									bomEntity.setXianbianCount(nowma);
								}else{
									bomEntity.setXianbianCount(null);
								}
								/*安全库存(查询cb物料表获得)*/
								bomEntity.setSafeCount(materielList.get(0).getRemark4());
								/*将领料单号和BOM绑定*/
								if (StringUtil.isNotEmpty(lingliaoOrdNum)) {
									bomEntity.setRemark02(lingliaoOrdNum);
								}else{
									bomEntity.setRemark02(null);
								}
								/*领料需求量((需求数量*损耗率)-线边库库存)*/
								bomEntity.setLingliaoRequireCount(String.valueOf((Integer.parseInt(plannum)-Integer.parseInt(nowma))));
								
								/**
								 * hy根据物料名称先查出其id再去lc的materialStock表中查询条码、批次号
								 */
								materialStock=iMaterService.sel(iMaterService.selMid(bomEntity.getMaterialName()));
								bomEntity.setRemark05(materialStock.getWremark1());//批次号
								bomEntity.setRemark06(materialStock.getWremark2());//条形码
								
								/*如果根据物料编号没有查到就向数据库添加新的信息*/
								bom2Service.insertBOM(bomEntity);
							}
						}
					}
				}
			}
			 response.getWriter().print(1);
		}else{
			response.getWriter().print(0);
		}		
	}
	
	/**
	 * 根据领料单号查询当前的BOM
	 * @param response
	 * @param request
	 * @param lingliaoOrder 领料单号
	 * @throws Exception
	 */
	@RequestMapping("/queryBOMInfo")
	public void queryBOMInfo(HttpServletResponse response, HttpServletRequest request, String lingliaoOrder) throws Exception{
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		String lingliaoOrder1 = new String(lingliaoOrder.getBytes("ISO8859-1"),"UTF-8");
		List<BOM> bomList = bom2Service.queryBOMInfo(lingliaoOrder1);
		JSONArray jsonArray = JSONArray.fromObject(bomList);
		JSONObject config = new JSONObject();
		config.put("rows", jsonArray);
		config.put("total", 10);
		ResponseUtil.write(response, config);
	}
}
